package com.campus.util;

import cn.hutool.core.bean.BeanUtil;
import com.campus.entity.bo.StudentAttendanceInfoBO;
import com.campus.service.UploadService;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

/**
 * @author LuTanhang
 */
@Component
@Slf4j
public class ExcelUtil {

    @Value("${qiniu.accessKey}")
    private  String accessKey;
    @Value("${qiniu.secretKey}")
    private  String secretKey;
    @Value("${qiniu.bucket}")
    private  String bucket;
    @Value("${qiniu.prefix}")
    private  String prefix;

    @Autowired
    private UploadService uploadService;

    private final String POSTFIX = ".xls";
    private static final String[] HEAD = new String[]{"学号","姓名","身份证号码","性别","宿舍信息","辅导员","邮箱","电话号码","出勤情况"};
    private static final String FILE_GENERATE_PATH = "/usr/share/data/excel/";
//    private static final String FILE_GENERATE_PATH = "D://";

    public  String generateExcel(List<StudentAttendanceInfoBO> data, String fileName) throws IOException {
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet("出勤情况");
        HSSFRow row = sheet.createRow(0);
        for (int i = 0; i < HEAD.length; i++) {
            row.createCell(i).setCellValue(HEAD[i]);
        }
        for (int x = 1; x <= data.size(); x++) {
            Row rowNew = sheet.createRow(x);
            for (int i = 0; i < data.size(); i++) {
                StudentAttendanceInfoBO record = data.get(x - 1);
                rowNew.createCell(0).setCellValue(record.getAccount()+"D");
                rowNew.createCell(1).setCellValue(record.getName());
                rowNew.createCell(2).setCellValue(record.getIdentity());
                rowNew.createCell(3).setCellValue(record.getSex());
                rowNew.createCell(4).setCellValue(record.getBedroom());
                rowNew.createCell(5).setCellValue(record.getTeacher());
                rowNew.createCell(6).setCellValue(record.getEmail());
                rowNew.createCell(7).setCellValue(record.getTel());
                rowNew.createCell(8).setCellValue(record.getStatus());
            }
        }
        //文件生成
        long currTime = System.currentTimeMillis();
        OutputStream outputStream = new FileOutputStream(FILE_GENERATE_PATH+currTime+POSTFIX);
        wb.write(outputStream);
        outputStream.flush();
        outputStream.close();
        //七牛云环境
//        uploadService.delete2FileFromQiniu(fileName+POSTFIX);
        LocalDateTime now = LocalDateTime.now();
        uploadService.upload2Qiniu(FILE_GENERATE_PATH+currTime+POSTFIX,fileName+now.format(DateTimeFormatter.ofPattern("(截止于HH时mm分ss秒)"))+POSTFIX);
        new File(FILE_GENERATE_PATH+currTime+POSTFIX).delete();
        return prefix+"/"+fileName+now.format(DateTimeFormatter.ofPattern("(截止于HH时mm分ss秒)"))+POSTFIX;
    }
}
